﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace CProd
{
    public partial class frmUtilizadores : Form
    {
        #region  Declarações

        SqlDataAdapter daUsers = new SqlDataAdapter();
        SqlCommandBuilder Builder = new SqlCommandBuilder();
        DataTable dtUsers = new DataTable("dtUsers");
        DataView dvUsers = new DataView();
        BindingSource srcUsers = new BindingSource();
        Boolean newRecord = false;

        #endregion

        public frmUtilizadores()
        {
            InitializeComponent();
        }

        private void frmUsers_Load(object sender, EventArgs e)
        {
            Builder.DataAdapter = daUsers;
            daUsers.SelectCommand = new SqlCommand("Select * from Utilizadores", frmEntrada.conn);
            daUsers.UpdateCommand = Builder.GetUpdateCommand();
            dtUsers.CaseSensitive = true;
            daUsers.Fill(dtUsers);

            srcUsers.DataSource = dtUsers.DefaultView;
            dvUsers.Table = dtUsers;

            txtCodigo.DataBindings.Add("Text", srcUsers, "Codigo");
            txtNome.DataBindings.Add("Text", srcUsers, "Nome");

            GridUsers.DataSource = srcUsers;
            GridUsers.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            GridUsers.Columns[0].HeaderText = "Código";
            GridUsers.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
        }

        private void btnSair_Click(object sender, EventArgs e)
        {
            Close();
            Dispose();
        }

        private void btFirst_Click(object sender, EventArgs e)
        { srcUsers.MoveFirst(); }

        private void btLast_Click(object sender, EventArgs e)
        { srcUsers.MoveLast(); }

        private void btNext_Click(object sender, EventArgs e)
        { srcUsers.MoveNext(); }

        private void btPrev_Click(object sender, EventArgs e)
        { srcUsers.MovePrevious(); }

        private void btInsert_Click(object sender, EventArgs e)
        {
            txtCodigo.ReadOnly = false;
            txtCodigo.Text = "";
            txtNome.Text = "";
            txtCodigo.Focus();
            newRecord = true;
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            if (txtCodigo.ReadOnly == false) txtCodigo.ReadOnly = true;

            try
            {
                #region novo registo

                if (newRecord)
                {
                    dvUsers.RowFilter = string.Format("Codigo='{0}'", txtCodigo.Text);
                    if (dvUsers.Count > 0)
                    {
                        MessageBox.Show("Já existe esse utilizador");
                        newRecord = false;
                    }
                    else
                    {
                        DataRow dRow = dtUsers.NewRow();
                        dRow[0] = txtCodigo.Text;
                        dRow[1] = txtNome.Text;
                        dtUsers.Rows.Add(dRow);
                    }
                }
                #endregion
                else
                {
                    BindingContext[srcUsers].EndCurrentEdit();
                }
                daUsers.Update(dtUsers);

                //Actualiza Dados Mostrados
                dtUsers.Clear();
                daUsers.Fill(dtUsers);
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }

        private void btDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Tem a certeza?", "Apagar Registo", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                try
                {
                    srcUsers.RemoveCurrent();
                    daUsers.Update(dtUsers);
                    MessageBox.Show("Registo Apagado com Sucesso");
                }
                catch (Exception ex)
                { MessageBox.Show(ex.Message); }
            }
        }
    }
}
